package com.digitalchina.lxnsy.activity.old;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.digitalchina.lxnsy.R;
import com.digitalchina.lxnsy.api.BusinessAgr;
import com.digitalchina.lxnsy.api.BusinessFile;
import com.digitalchina.lxnsy.base.BaseActivity;
import com.digitalchina.lxnsy.constant.MSG;
import com.digitalchina.lxnsy.entity.ExperienceVO;
import com.digitalchina.lxnsy.entity.FileVO;
import com.digitalchina.lxnsy.loader.GlideImageLoader;
import com.digitalchina.lxnsy.toolkit.FastJsonUtil;
import com.digitalchina.lxnsy.toolkit.FrescoUtil;
import com.digitalchina.lxnsy.toolkit.StringUtil;
import com.digitalchina.lxnsy.xml.LocationXML;
import com.facebook.drawee.view.SimpleDraweeView;
import com.lzy.imagepicker.ImagePicker;
import com.lzy.imagepicker.bean.ImageItem;
import com.lzy.imagepicker.ui.ImageGridActivity;

import java.util.ArrayList;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * ================================================
 * 项目名称：BigData
 * 类描述：
 * 包名：com.digitalchina.bigdata.activity
 * 创建人：郑皓
 * 创建时间：2019/4/17 19:15
 * 修改备注：
 * ================================================
 */
public class AddExperienceActivity extends BaseActivity {

    @Bind(R.id.et_title)
    EditText etTitle;
    @Bind(R.id.et_content)
    EditText etContent;
    @Bind(R.id.sdv_img)
    SimpleDraweeView sdvImg;
    @Bind(R.id.btn_submit)
    Button btnSubmit;

    private String disPestsWeedsId = "";
    private String id = ""; // 经验ID
    private String img = ""; // 图片地址

    public static final int REQUEST_CODE_SELECT = 100;

    @Override
    public void setLayout() {
        setContentLayout(R.layout.activity_add_exp);
    }

    @Override
    public void initMember() {

        ImagePicker imagePicker = ImagePicker.getInstance();
        imagePicker.setImageLoader(new GlideImageLoader());   //设置图片加载器
        imagePicker.setShowCamera(true);  //显示拍照按钮
        imagePicker.setMultiMode(false);    //是否多选模式
        imagePicker.setCrop(false);        //允许裁剪（单选才有效）


        if (getIntent().hasExtra("disPestsWeedsId")) {
            setTitle("新增经验");
            disPestsWeedsId = (String) getIntent().getSerializableExtra("disPestsWeedsId");
        } else {
            ExperienceVO vo = (ExperienceVO) getIntent().getSerializableExtra("ExperienceVO");
            id = vo.getId();
            disPestsWeedsId = vo.getDisPestsWeedsId();
            etTitle.setText(vo.getTitle());
            etContent.setText(vo.getControlMethod());
            img = vo.getImg();
            FrescoUtil.showImageSmall(img, sdvImg);
        }
    }

    @Override
    public void addListener() {

        sdvImg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(AddExperienceActivity.this, ImageGridActivity.class);
                startActivityForResult(intent, REQUEST_CODE_SELECT);
            }
        });
        btnSubmit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                save();
            }
        });

    }

    private void save() {

        String title = etTitle.getText().toString();
        String content = etContent.getText().toString();

        String address = LocationXML.getLocationAddress(this);

        if (StringUtil.isStrEmpty(title)) {
            showToast("请输入标题");
            return;
        }
        if (StringUtil.isStrEmpty(content)) {
            showToast("请输入防治方法");
            return;
        }
        if (StringUtil.isStrEmpty(img)) {
            showToast("请上传图片");
            return;
        }

        BusinessAgr.saveExperience(this, id, title, content, img, address,
                disPestsWeedsId, mHandler);

    }

    @Override
    public void setHandler() {
        mHandler = new Handler(new Handler.Callback() {
            @Override
            public boolean handleMessage(Message msg) {
                switch (msg.what) {
                    case MSG.MSG_FILE_UPLOAD_SUCCESS:  // 上传图片
                        dismissDialog();
                        FileVO fileVO = FastJsonUtil.getBean(msg.obj.toString(), "body", "fileStorage", FileVO.class);
                        if (fileVO == null)
                            return false;

                        img = fileVO.getName();
                        FrescoUtil.showImageSmall(img, sdvImg);
                        break;
                    case MSG.MSG_FILE_UPLOAD_FIELD:
                        dismissDialog();
                        showToast(msg.obj.toString());
                        break;
                    case MSG.MSG_SAVE_EXPERIENCE_SUCCESS:
                        showToast("发布成功");
                        setResult(RESULT_OK);
                        finish();
                        break;
                    case MSG.MSG_SAVE_EXPERIENCE_FIELD:
                        showToast(msg.obj.toString());
                        break;
                }
                return false;
            }
        });
    }

    @Override
    public void callBack(Object object) {

    }

    @Override
    public void rightOnClick() {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    /**
     * 跳转回调
     *
     * @param requestCode
     * @param resultCode
     * @param data
     */
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == ImagePicker.RESULT_CODE_ITEMS) {
            //添加图片返回
            if (data != null && requestCode == REQUEST_CODE_SELECT) {
                ArrayList<ImageItem> images = (ArrayList<ImageItem>) data.getSerializableExtra(ImagePicker.EXTRA_RESULT_ITEMS);
                if (images != null && images.size() > 0) {
                    showDialog("正在上传图片...");
                    BusinessFile.imageCompressAndUpload(this, images.get(0).path, mHandler);
                }
            }
        }

    }

}
